$(document).ready(function () {
    findStudent()
});

//下拉列表获取课程类别
function courseType() {
    var cType = $("#courseType");
    $.ajax({
        url: "http://localhost:8080/mark/selectCourseType",
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            var str = "";
            d = data.data;
            ctype = d[1]
            for (x in ctype) {
                str += "<option value='" + ctype[x].courseType + "'>" + ctype[x].courseType + "</option>"
            }
            cType.append(str);
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
};

//通过课程类型查看课程名称
function couresName(courseType) {

    var courseName = $("#courseName")
    $.ajax({
        url: "http://localhost:8080/mark/selectCourseNameByCoursetype?courseType=" + courseType,
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            // alert("aa")
            var str = "";
            d = data.data.courseList;
            for (x in d) {
                str += "<option value='" + d[x].courseName + "'>" + d[x].courseName + "</option>"
            }
            courseName.html(str);
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
};


//查找所有学生
function findStudent() {
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.studentList",
        "sAjaxSource": "http://localhost:8080/mark/queryAllStudent",
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<input type='text' class='studentmark' name='studentmark'/>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "stuNum"},
            {"mDataProp": "stuName"},
            {"mDataProp": ""},
        ],
    })

}

//录入成绩
$(document).on('click', '#addMarkBtn', function () {
    // 获取FrontPageAddUpdateMark页面超链接的参数 --这样调用：
    var termYear = getQueryString("termYear");
    var termNum = getQueryString("termNum");
    var courseType = getQueryString("courseType");
    var courseName = getQueryString("courseName");
    var courseId = getQueryString("courseNum");
    /*if (termYear == "" && termNum == "") {
        layer.alert("请选择学年和学期");
        return false;
    }
    if (termYear == "" && termNum != "") {
        layer.alert("请选择学年");
        return false;
    }
    if (termNum == "" && termYear != "") {
        layer.alert("请选择学期");
        return false;
    }*/
    var studentmark = $(".studentmark");
    var flag = 0;
    for (var i = 0; i < studentmark.length; i++) {
        var reg = new RegExp("^(\\d|[1-9]\\d|100)$");
        var stuName1 = $(studentmark[i]).parents("tr").children(0).eq(1).text();
        var mark3 = $(studentmark[i]).val();
        if(!reg.test(mark3)) {
            layer.alert("学生"+stuName1+"成绩请输入0-100的整数！");
            flag = 1;
            return false;
        }
    }

    for (var i = 0; i < studentmark.length; i++) {
        //alert("studentmark.length--"+studentmark.length)
        var mark = $(studentmark[i]).val();
        var stuNum = $(studentmark[i]).parents("tr").children(0).eq(0).text();
        var stuName = $(studentmark[i]).parents("tr").children(0).eq(1).text();

        //var testNum = 101;


        if (mark == ""||flag==1) {
           // layer.alert("成绩不能为空！");
            return false;
        }
        //alert($(studentmark[i]).parents("tr").children(0).eq(0).text())
        //alert($(studentmark[i]).parents("tr").children(0).eq(1).text())
        $.ajax({
            url: "http://localhost:8080/mark/addMark?termYear=" + termYear + "&termNum=" + termNum +
                                                                        "&courseType=" + courseType + "&courseName=" + courseName +
                                                                        "&mark=" + mark + "&stuNum=" + stuNum + "&stuName=" + stuName +
                                                                        "&courseId=" + courseId,
            type: "GET",
            async: false,
            dataType: "json",
            success: function (data) {
                layer.alert("添加成功!");
                //location.href='FrontPageAddUpdateMark.html';
            },
            error: function () {
                layer.alert("你的网络太慢了！添加失败!再试一次吧！");
            }
        });
    }
})

//获取addMark.html的url地址参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        //return unescape(r[2]);
        //decodeURI解码
        return decodeURI(decodeURI(r[2]));
    }
    return null;
}


//查找还没有录成绩的学生
/*function queryNoScoreStudent() {
    var table = $('#dataTable').dataTable()
    if (table) {
        table.fnDestroy();
    }
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.studentList",
        "sAjaxSource": "http://localhost:8080/mark/queryAllStudent",
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<input type='text' class='studentmark' name='studentmark'/>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "stuNum"},
            {"mDataProp": "stuName"},
            {"mDataProp": ""},
        ],
    })

}*/
